<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img
          class="logo-img"
          src="../assets/rabait.png"
          alt=""
        />
        <span>电商后台管理系统</span>
      </div>
      <el-button
        type="info"
        @click="logout"
      >退出</el-button>
    </el-header>
    <!-- 内容区域 -->
    <el-container>
      <!-- 左侧导航边栏 -->
      <el-aside :width="isCollpase ? '64px' : '200px'">
        <div
          class="toggle-button"
          @click="toggleCollapse"
        >|||</div>
        <!-- 侧边栏菜单 -->
        <el-menu
          background-color=" #1c2938"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse="isCollpase"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <!-- 1级菜单 -->
          <!-- index在每一项菜单中都是唯一确定的,并且index的值类型是字符串 -->
          <el-submenu
            :index="item.id+''"
            v-for="item in menuList"
            :key="item.id"
          >
            <!-- 菜单模板 -->
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 2级菜单 -->
            <el-menu-item
              :index="'/'+ subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/'+ subItem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧主体区域 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menuList: [],
      // 图标对象
      iconsObj: {
        125: 'el-icon-s-custom',
        103: 'el-icon-s-check',
        101: 'el-icon-s-shop',
        102: 'el-icon-s-order',
        145: 'el-icon-s-marketing'
      },
      // 是否折叠
      isCollpase: false,
      // 被激活的链接地址
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有的菜单
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      // 给菜单数组赋值,未接收到时返回错误结果,接收到时赋值
      if (res.meta.status !== 200) {
        return this.$message.console.error(res.meta.msg)
      } else {
        this.menuList = res.data
      }
      // console.log(res)
    },
    // 点击按钮切换菜单的折叠与展开
    toggleCollapse() {
      this.isCollpase = !this.isCollpase
    },
    // 保存链接的激活状态，解决侧边栏高亮问题
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #2b333e;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #ffffff;
  font-size: 20px;
  font-weight: 600;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.logo-img {
  width: 50px;
  height: 50px;
}
.el-aside {
  background-color: #1c2938;
  // 解决左侧菜单不能对齐的问题
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaf4fa;
}
.toggle-button {
  background-color: #313a52;
  font-size: 10px;
  line-height: 24px;
  text-align: center;
  color: #eaf4fa;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
